<?php

/* 
 * 系统管理类
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class SysController extends AppController
{
    public function __construct() {
        parent::__construct();
    }
    public function sql(){
        $this->title = 'SQL';
        $this->display('sql');
    }
    public function sql_query(){
        $this->_sql_query();
        $this->display('sql');
    }
    private function _sql_query() {        
        $sql = stripslashes($_POST['sql']);
        if(empty($sql)) {
            return;
        }
        $this->assign('sql', $sql);
        /* 解析查询项 */
        $sql = str_replace(array('\n','\r','\t'), '', $sql);
        $query_items = explode(";", $sql);
        foreach ($query_items as $key=>$value)
        {
            if (empty($value))
            {
                unset($query_items[$key]);
            } else {
                $value = str_replace(array('\n','\r','\t'), '', $value);
                $query_items[$key] = trim($value);
            }
        }
        /* 如果是多条语句，拆开来执行 */
        if (count($query_items) > 1)
        {
            foreach ($query_items as $key=>$value)
            {
                if (sysModel::load()->query_sql($value))
                {
                    $this->assign('type',  1);
                }
                else
                {
                    $this->assign('type',  -1);
                    $this->assign('error', sysModel::load()->error());
                    return;
                }
            }
            return; //退出函数
        }

        /* 单独一条sql语句处理 */
        if (preg_match("/^(?:UPDATE|DELETE|TRUNCATE|ALTER|DROP|FLUSH|INSERT|REPLACE|SET|CREATE)\\s+/i", $sql))
        {
            if (sysModel::load()->query_sql($sql))
            {
                $this->assign('type',  1);
            }
            else
            {
                $this->assign('type',  0);
                $this->assign('error', sysModel::load()->error());
            }
        }
        else
        {
            $data = sysModel::load()->get_all($sql);
            if ($data === false)
            {
                $this->assign('type',  0);
                $this->assign('error', sysModel::load()->error());
            }
            else
            {
                $result = '';
                if (is_array($data) && isset($data[0]) === true)
                {
                    $result = "<table class='table text-center table-bordered table-hover'>\n <tr>";
                    $keys = array_keys($data[0]);
                    for ($i = 0, $num = count($keys); $i < $num; $i++)
                    {
                        $result .= "<th class='text-center'>" . $keys[$i] . "</th>\n";
                    }
                    $result .= "</tr> \n";
                    foreach ($data AS $data1)
                    {
                        $result .= "<tr>\n";
                        foreach ($data1 AS $value)
                        {
                            $result .= "<td>" . $value . "</td>";
                        }
                        $result .= "</tr>\n";
                    }
                    $result .= "</table>\n";
                }
                else
                {
                    $result ="<center><h3>数据为空</h3></center>";
                }

                $this->assign('type',   2);
                $this->assign('result', $result);
            }
        }
    }
}

